import { CategoryList,BannerList,getGoods } from '@/api/home'
import { onMounted,ref,type Ref } from 'vue'
import {categoryListStore } from '@/stores/categoryList'
import { requestSearch } from '@/api/search'
import type {IGoods,IBanner } from '@/types/home'
import { searchStore } from "@/stores/searchDate";
import { useRouter } from 'vue-router'

// 分类标签
export const useCateogry = () =>{
let list:Ref<Array<IGoods>> = ref([])
const active = ref(0)

const bindCategory =async ()=>{
  const data = await CategoryList()
  const {resultCode,resultInfo } = data
  if (resultCode === 1) {
    const counter = categoryListStore()
    counter.saveCategoryList(resultInfo.list)
    list.value = counter.category
  }
}

onMounted(()=>{
  bindCategory()
})
return {active,list}
}

// 轮播图
export const useBanner = ()=>{
  const bannerList:Ref<Array<IBanner>> = ref([])
  const bindBanner = async () =>{
    const bannerData = await BannerList()
    const {resultCode,resultInfo } = bannerData
    if (resultCode === 1) {
      bannerList.value = resultInfo.list 
  }
}
onMounted(()=>{
  bindBanner()
})
return {bannerList}
}

// 商品列表
const goodsList = ref([])
export const useGoods = () =>{
  
  const bindGoods = async () =>{
    const bannerData = await getGoods()
    const {resultCode,resultInfo } = bannerData
    if (resultCode === 1) {
      goodsList.value = resultInfo.list 
  }
 
}
onMounted(()=>{
  bindGoods()
})
return {goodsList}
}

// 搜索模块
export const useSearch =()=>{
  const router = useRouter()
  const goodsSearch = () =>{
    router.push('/search')
  }
  return { goodsSearch }
}

